<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ParametersController extends Controller
{
	//引入商品参数模板
	public function index(){
		$result=DB::table('parameters')
		->join('goods','parameters.gid','=','goods.gid')
		->get();
		return view('goods.parameters',['par'=>$result]);
	}
	
	//显示商品参数模板
	public function addPar($gid){
		//根据商品ID查询商品名
		$result[0]=DB::table('goods')->where('gid','=',$gid)->first();
		//dd($result[0]);
		//引入模板
		return view('goods.addparameters',['goods'=>$result[0]]);
	}
	
	
	//添加参数
	public function insertPar(Request $request){
		//接受表单传过来的值
		$result=$request->only('gid','design','caizhi','color','season','style');
		
		//插入数据库
		$par=DB::table('parameters')->insert($result);
		
		if($par){
			return redirect('/goods/parameters');
		}else{
			return redirect('/goods/insertPar');
		}
	}
	
	//删除商品参数
	public function deletePar($id){
		$result=DB::table('parameters')->where('id', '=', $id)->delete();
		if($result){
			return redirect('/goods/parameters');
		}else{
			return redirect('/goods/parameters/{$id}');
		}
	}
	
	//修改商品参数
	public function updatePar($id){
		//查询数据
		//$result=DB::table('goods')
		///->join('parameters', 'goods.id', '=', 'parameters.id')
		//->get();
		$result=DB::table('parameters')
		->join('goods','parameters.gid','=','goods.gid')
		->get();
		//dd($result[0]);
		return view('goods.updateparameters',['goods'=>$result[0]]);
	}
	
	//修改商品参数完成
	public function updateParing(Request $request){
		//接收post表单传递过来的值
		$result=$request->only(['design','caizhi','color','season','style']);
		$result2=$request->only(['id']);
		//修改数据
		$result1=DB::table('parameters')->where('id','=',$result2)->update($result);
		//判断
		if($result1){
			return redirect('/goods/parameters');
		}else{
			return redirect("goods/updateParing");
		}
	}
}
